home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uwsrc.arc / WINSUBR.C < prev   
C/C++ Source or Header  |  1989-04-29  |  29KB  |  1,259 lines

  1. /* subroutines for multi-window terminal emulation
  2.  */
  3.  
  4. #include <obdefs.h>
  5. #include <gemdefs.h>
  6. #include <osbind.h>
  7. #include <stdio.h>
  8. #include "wind.h"
  9. #include "windefs.h"
  10.  
  11. extern int handle;
  12.  
  13. /* variables used by various routines
  14.  */
  15.  
  16. long dummy;                /* dummy return variable */
  17. extern    int    outwind, outport;    /* window selection */
  18. extern    int    scr_x, scr_y;        /* size of the screen */
  19. extern    int    scr_w, scr_h;
  20. extern    int    fast;            /* flag for fast open/close */
  21. extern    int    overstrike;
  22. extern    int    sliders;        /* flag for sliders on new windows */
  23. extern    int    titles;            /* flag for title bars on new windows */
  24. int    tmp;                /* temporary for anything... */
  25. extern    char    alert[300];        /* used for alerts */
  26. extern    FNT    *curfont;        /* current font */
  27. extern    MFDB    screen_mf;        /* screen descriptor */
  28. extern    int    mouse;            /* for mouse on/off */
  29. extern    int    audibell;        /* What happens on BEL? */
  30. extern    int    visibell;
  31. extern    int    toponbel;
  32.  
  33. struct    wi_str    w[MAX_WIND];
  34.  
  35. /* the program code...
  36.  */
  37.  
  38. char *getmem(size)
  39. register long size;
  40. {
  41.   char *got;
  42.  
  43.   got = (char *) Malloc(size);
  44. #ifdef DEBUG
  45.   printf("alloc returned %lx of %ld size\n", got, size);
  46. #endif
  47.   if (got == NULL)
  48.   {
  49.     sprintf(alert, "[1][Could not get %ld bytes][Ok]", size);
  50.     form_alert(1, alert);
  51.   } else
  52.   {
  53.     bzero(got, size);
  54.   }
  55.   return got;
  56. }
  57.  
  58. bzero(ptr, size)
  59. register char *ptr;
  60. register long size;
  61. {
  62.   while (size--)
  63.     *ptr++ = 0;
  64. }
  65.  
  66. /* find_port maps from window handles to UW port identifiers
  67.  */
  68.  
  69. find_port(wnd)
  70. {
  71.   return w[wnd].port;
  72. }
  73.  
  74. /* find_wind maps from port ids to window handles
  75.  */
  76.  
  77. find_wind(port)
  78. {
  79. int i;
  80.  
  81.   for (i=1; i<MAX_WIND; i++)
  82.     if (w[i].port == port) return i;
  83.   return 0;
  84. }
  85.  
  86. /* w_open opens a window with the supplied name and port and returns a port
  87.  * number.  If port was zero, a free port is chosen.
  88.  */
  89.  
  90. w_open(port, name, xsiz, ysiz)
  91. char *name;
  92. {
  93.   register struct wi_str *wp;
  94.   int wdes;
  95.   int i, cnt, wtyp;
  96.   int tmp_x, tmp_y, tmp_w, tmp_h;
  97.  
  98.   if (port && find_wind(port)) return port;  /* this window is already open */
  99.   if (!port)
  100.   {
  101.     for (i=1; i<MAX_WIND; i++)
  102.     {
  103.       if (!find_wind(i))
  104.       {
  105.         port = i;        /* a free port */
  106.         break;
  107.       }
  108.     }
  109.   }
  110.  
  111.   wtyp = (sliders ? WI_WITHSLD : 0) | (titles ? WI_NOSLD : 0);
  112.   wind_calc(0, wtyp, 0, 0, curfont->inc_x*xsiz+2*X0,
  113.     curfont->inc_y*ysiz+2*Y0, &dummy, &dummy, &tmp_w, &tmp_h);
  114.   if (tmp_w>scr_w)
  115.     tmp_w = scr_w;    /* full size <= screen size */
  116.   tmp_x = 10*(port-1);
  117.   if (tmp_h>scr_h)
  118.     tmp_h = scr_h;
  119.   tmp_y = scr_y+16*(port-1);
  120.  
  121.   wdes = wind_create(wtyp, tmp_x, tmp_y, tmp_w,
  122.     tmp_h);
  123.   if (wdes < 0)
  124.   {
  125.     form_alert(1, "[1][Sorry, GEM has|no more windows|for us...][Ok]");
  126.     return 0;
  127.   }
  128.   wp = &w[wdes];
  129.   wp->wi_w = X0*2 + curfont->inc_x*xsiz;
  130.   wp->wi_h = Y0*2 + curfont->inc_y*ysiz;
  131.   wp->port = port;
  132.   if (!fast)
  133.     graf_growbox(0, 0, 20, 10, tmp_x, tmp_y, tmp_w, tmp_h);
  134.   wind_open(wdes, tmp_x, tmp_y, tmp_w, tmp_h);
  135.   wind_get(wdes, WF_WORKXYWH, &wp->x, &wp->y, &wp->w, &wp->h);
  136.   wp->fulled = 0;
  137.   wp->used = 1;
  138.   wp->x_off = 0;
  139.   wp->y_off = 0;
  140.   wp->px_off = 0;
  141.   wp->py_off = 0;
  142.   wp->m_off = wp->x & 15;
  143.   wp->cur_x = X0;
  144.   wp->cur_y = Y0;
  145.   wp->top_y = Y0;
  146.   wp->font = curfont;
  147.   wp->x_chrs = xsiz;
  148.   wp->y_chrs = ysiz;
  149.   wp->wi_mf.wpix = 2*X0 + xsiz*curfont->inc_x;
  150.   wp->wi_mf.hpix = 2*Y0 + ysiz*curfont->inc_y;
  151. #ifdef    KOPY
  152.   wp->wi_mf.wwords = ((wp->wi_mf.wpix>>5) +1) << 1;
  153. #else
  154.   wp->wi_mf.wwords = (wp->wi_mf.wpix>>4) +1;
  155. #endif
  156.   wp->wi_mf.ptr = getmem(((long)wp->wi_mf.hpix+wp->font->inc_y*MAXSCROLLED)
  157.                 *wp->wi_mf.wwords*2);
  158.   wp->wi_mf.format = 0;
  159.   wp->wi_mf.planes = 1;
  160.   wp->ptr_status = LOG_NONE;
  161.   wp->wi_style = wtyp;
  162.   w_rename(wdes, name);
  163.   strcpy(wp->wi_fpath, ".\\*.*");
  164.   wp->top_age = 1;
  165.   for (cnt = 1; cnt < MAX_WIND; cnt++)
  166.     if (w[cnt].port != 0)
  167.       w[cnt].top_age++;
  168.   
  169.   setvslide(wdes);
  170.   sethslide(wdes);
  171.   
  172.   return wp->port;
  173. }
  174.  
  175. /* w_closei removes a window but does not release its storage.
  176.  * This is used if the window contents must be saved for later use.
  177.  */
  178.  
  179. w_closei(wdes)
  180. {
  181.   int xx, yy, ww, hh;
  182.   register struct wi_str *wp = &w[wdes];
  183.  
  184.   if (!wp->used) return;
  185.   if (wp->wi_lfd)
  186.     fclose(wp->wi_lfd);
  187.   wind_get(wdes, WF_CURRXYWH, &xx, &yy, &ww, &hh);
  188.   wind_close(wdes);
  189.   if (!fast)
  190.     graf_shrinkbox(0, 0, 20, 10, xx, yy, ww, hh);
  191.   wind_delete(wdes);
  192. }
  193.  
  194. /* w_close removes a window.  Most work is done by GEM, although w_close
  195.  * does some cleanup functions, too.
  196.  */
  197.  
  198. w_close(wdes)
  199. {
  200.   register struct wi_str *wp = &w[wdes];
  201.  
  202.   if (!wp->used) return;
  203.   w_closei(wdes);
  204.   Mfree(wp->wi_mf.ptr);
  205.   bzero(wp, (long)sizeof (struct wi_str));
  206. }
  207.  
  208. /* w_resize resizes an existing window.
  209.  */
  210.  
  211. w_resize(wdes, xsiz, ysiz)
  212. {
  213.   register struct wi_str *wp1 = &w[wdes];
  214.   struct wi_str *wp2;
  215.   struct wi_str ws;
  216.   static int c[8];
  217.   int port, wind, i;
  218.  
  219.   if (!wp1->used) return;
  220.   ws = *wp1;
  221.   port = find_port(wdes);
  222.   w_closei(wdes);
  223.   bzero(wp1, (long)sizeof (struct wi_str));
  224.   port = w_open(port, "", xsiz, ysiz);
  225.   wind = find_wind(port);
  226.   wp2 = &w[wind];
  227.   c[0] = ws.m_off;
  228.   c[1] = ws.top_y + max(0, ws.wi_mf.hpix - wp2->wi_mf.hpix);
  229.   c[2] = c[0] + min(ws.wi_mf.wpix, wp2->wi_mf.wpix);
  230.   c[3] = c[1] + min(ws.wi_mf.hpix, wp2->wi_mf.hpix);
  231.   c[4] = wp2->m_off;
  232.   c[5] = wp2->top_y;
  233.   c[6] = c[4] + min(ws.wi_mf.wpix, wp2->wi_mf.wpix);
  234.   c[7] = c[5] + min(ws.wi_mf.hpix, wp2->wi_mf.hpix);
  235.   /* copy screen */
  236.   vro_cpyfm(handle, FM_COPY, c, &ws.wi_mf, &wp2->wi_mf);
  237.   /* copy parameters */
  238.   wp2->inverse = ws.inverse;
  239.   wp2->insmode = ws.insmode;
  240.   if (wp2->font != ws.font)
  241.   {
  242.     wp2->cur_x = X0;
  243.     wp2->cur_y = (wp2->y_chrs - 1) * wp2->font->inc_y + Y0;
  244.   }
  245.   else
  246.   {
  247.     wp2->cur_x = (wp2->x_chrs - 1) * wp2->font->inc_x + X0;
  248.     if (ws.cur_x < wp2->cur_x)
  249.       wp2->cur_x = ws.cur_x;
  250.     wp2->cur_y = max(0, ws.cur_y - c[1]) + Y0;
  251.   }
  252.   wp2->state = ws.state;
  253.   for (i=0; i<80; i++) wp2->nuname[i] = ws.nuname[i];
  254.   for (i=0; i<80; i++) wp2->wi_fpath[i] = ws.wi_fpath[i];
  255.   for (i=0; i<20; i++) wp2->wi_fname[i] = ws.wi_fname[i];
  256.   wp2->nuptr = ws.nuptr;
  257.   wp2->ptr_status = ws.ptr_status;
  258.   wp2->wi_lfd = ws.wi_lfd;
  259.   wp2->kerm_act = ws.kerm_act;
  260.   w_rename(wind, ws.name);
  261.   
  262.   Mfree(ws.wi_mf.ptr);
  263.   return port;
  264. }
  265.  
  266. /* w_rename changes the title bar of a window
  267.  */
  268.  
  269. w_rename(wdes, name)
  270. char *name;
  271. {
  272.   register struct wi_str *wp = &w[wdes];
  273.  
  274.   if (name)
  275.     strcpy(wp->name, name);
  276.   if (wp->wi_style & NAME)
  277.   {
  278.     sprintf(wp->dname, " %s%s %s ", (wp->ptr_status != LOG_NONE)? "\275": "",
  279.       wp->wi_lfd? "\237": "", wp->name);
  280.     wind_set(wdes, WF_NAME, wp->dname + (wp->dname[1] == ' '), 0, 0);
  281.   }
  282. }
  283.  
  284. /* w_redraw redraws part of the screen from window contents.
  285.  * The coordinates are screen relative.
  286.  */
  287.  
  288. w_redraw(wdes, logic, xx, yy, ww, hh)
  289. {
  290.   static int c[8];
  291.   static GRECT t1, t2;
  292.   register struct wi_str *wp = &w[wdes];
  293.  
  294.   if (xx+ww > scr_w)
  295.     ww = scr_w - xx;
  296.   if (yy+hh > scr_h+scr_y)
  297.     hh = scr_h+scr_y - yy;
  298.   t2.g_x = xx; t2.g_y = yy;
  299.   t2.g_w = ww; t2.g_h = hh;
  300.   t1.g_x = wp->x; t1.g_y = wp->y;
  301.   t1.g_w = wp->w; t1.g_h = wp->h;
  302.   if (!rc_intersect(&t2, &t1)) return;    /* nothing to do... */
  303.   wind_update(TRUE);
  304.   wind_get(wdes, WF_FIRSTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
  305.   while (t1.g_w && t1.g_h)
  306.   {
  307.     if (rc_intersect(&t2, &t1))
  308.     {
  309.       if (mouse)
  310.       {
  311.     /* we have to do graphics, so switch the mouse off.
  312.      * mouse will be switched on again in main loop.
  313.      * this is ugly, but it improves speed a bit...
  314.      */
  315.     mouse = 0;
  316.     graf_mouse(M_OFF, NULL);
  317.       }
  318. #ifdef    KCOPY
  319.       tfbmr(wp->wi_mf.ptr, t1.g_x - wp->x + wp->x_off + wp->m_off,
  320.     t1.g_y - wp->y + wp->y_off + wp->top_y - Y0, wp->wi_mf.wwords >> 1,
  321.     screen_mf.ptr, t1.g_x, t1.g_y, screen_mf.wwords >> 1,
  322.     t1.g_w, t1.g_h, logic);
  323. #else
  324.       c[0] = t1.g_x - wp->x + wp->x_off + wp->m_off;
  325.       c[1] = t1.g_y - wp->y + wp->y_off + wp->top_y - Y0;
  326.       c[2] = c[0] + t1.g_w - 1;
  327.       c[3] = c[1] + t1.g_h - 1;
  328.       c[4] = t1.g_x;
  329.       c[5] = t1.g_y;
  330.       c[6] = c[4] + t1.g_w - 1;
  331.       c[7] = c[5] + t1.g_h - 1;
  332.       vro_cpyfm(handle, logic, c, &wp->wi_mf, &screen_mf);
  333. #endif
  334.     }
  335.     wind_get(wdes, WF_NEXTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
  336.   }
  337.   if (!fast && !mouse)
  338.   {
  339.     mou